import { Observable } from 'rxjs';
import { HttpHeaders, HttpParams } from '@angular/common/http';
import { SafeResourceUrl } from '@angular/platform-browser';

export type UploadObject = { [key: string]: any } | ((file: UploadFile) => { [key: string]: any });

export type UploadType = 'select' | 'drag' | 'picture';

export type UploadMethod = 'get' | 'post' | 'put' | 'GET' | 'POST' | 'PUT';

export enum UploadContentModeEnum {
  SELECT = 'select',
  DRAG = 'drag',
  CUSTOM = 'custom',
  PICTURE = 'picture',
}

// 单个文件的上传状态
export enum UploadFileStatusEnum {
  SELECT = 'select',
  READY = 'ready',
  PROGRESS = 'progress',
  SUCCESS = 'success',
  ERROR = 'error',
  CANCEL = 'cancel', // beforeUpload false 上传前取消
  REMOVE = 'remove',
}

// 整个上传的状态
export enum UploadStatusEnum {
  INIT = 'init', // 初始化
  READY = 'ready',
  PROGRESS = 'progress',
  SUCCESS = 'success',
  ERROR = 'error',
  COMPLETE = 'complete', // 已完成回现时使用
}

// 增强的文件类型
export interface UploadFile {
  [key: string]: any;
  key: string; // 文件唯一标示
  lastModified?: number;
  lastModifiedDate?: Date;
  name: string;
  size: number; // B
  fileSize?: string; // 换算后的单位
  type: string;
  webkitRelativePath?: string;
  status?: UploadFileStatusEnum; // 文件状态
  progress?: number; // 上传进度
  response?: any; // 上传成功返回结果
  error?: any; // 上传失败返回结果
  errorTip?: string; // 上传失败原因提示信息
  originFile?: File;
  url?: string | SafeResourceUrl;
}

export interface UploadFilter {
  name: string; // filter name
  filterFn: (files: UploadFile[]) => UploadFile[] | Observable<UploadFile[]>; // 处理的方法
}

export interface UploadHttpRequestOptions {
  url: string;
  method: string;
  file: UploadFile;
  originFile: File;
  httpHeaders: HttpHeaders;
  headers: UploadObject;
  formData: FormData;
  data: UploadObject;
  withCredentials: boolean;
  params?: HttpParams;
  responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';
  onSend: (file: UploadFile) => void; // 开始
  onProgress: (progress: number, file: UploadFile) => void; // 上传中
  onSuccess: (response: any, file: UploadFile) => void; // 成功
  onError: (error: any, file: UploadFile) => void; // 失败
}
// 上传最大限制单位
export enum UploadUnitEnum {
  KB = 'KB',
  MB = 'MB',
  GB = 'GB',
  BYTE = 'B',
}
